package com.retoc.oawx.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.retoc.oawx.common.util.R;
import com.retoc.oawx.config.shiro.JWTUtil;
import com.retoc.oawx.domain.CarUse;
import com.retoc.oawx.domain.Materials;
import com.retoc.oawx.domain.req.*;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.mapper.MaterialsMapper;
import com.retoc.oawx.service.CarUseService;
import com.retoc.oawx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/car")
@Api("用车申请模块")
public class UseCarController {

    @Autowired
    JWTUtil jwtUtil;

    @Autowired
    UserService userService;

    @Autowired
    CarUseService carUseService;

    @Autowired
    MaterialsMapper materialsMapper;


    @PostMapping("/useCarRequest")
    @ApiOperation("创建用车申请单")
    public R useCarRequest(@RequestBody @Valid CarUseReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);

        CarUse carUse=new CarUse();
        Boolean driver = req.getDriver();
        if (driver){
            carUse.setDriver(5);
        }
//        carUse.setDriver(req.getDriver());
        carUse.setCreateTime(DateUtil.date());
        carUse.setCreaterId(userId);
        carUse.setUuid(UUID.randomUUID().toString());
        DateTime start = DateUtil.parse(req.getStartTime());
        DateTime end = DateUtil.parse(req.getEndTime());
        carUse.setStartTime(start);
        carUse.setEndTime(end);
        carUse.setCarId(req.getCarId());
        carUse.setStatus(0);
        carUse.setReason(req.getReason());
        int row = carUseService.createCarUse(carUse);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        return R.ok();
    }

    @PostMapping("/useCarUpdate")
    @ApiOperation("创建用车申请单")
    public R useCarUpdate(@RequestBody @Valid CarUseUpdateReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);

        CarUse carUse=new CarUse();
        Boolean driver = req.getDriver();
        if (driver){
            carUse.setDriver(5);
        }
//        carUse.setDriver(req.getDriver());
        carUse.setCreaterId(userId);
        carUse.setId(req.getId());
        carUse.setCreateTime(DateUtil.date());
        DateTime start = DateUtil.parse(req.getStartTime());
        DateTime end = DateUtil.parse(req.getEndTime());
        carUse.setStartTime(start);
        carUse.setEndTime(end);
        carUse.setCarId(req.getCarId());
        carUse.setStatus(0);
        int row = carUseService.updateCarUse(carUse);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        return R.ok();
    }

    @PostMapping("/searchCarUseByCreater")
    @ApiOperation("查询自己的用车申请")
    public R searchCarUseByCreater(@RequestBody @Valid PageReq req, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        Integer page = req.getPage();
        Integer length = req.getLength();
        Boolean old = req.getOld();
        int start = (page - 1) * length;
        HashMap map = new HashMap<>();
        map.put("createrId", userId);
        map.put("start", start);
        map.put("length", length);
        ArrayList<HashMap> result=new ArrayList<>();
        if (old){
            result=carUseService.selectLeaveListByCreator(map);
        }else {
            result=carUseService.selectOldLeaveListByCreator(map);
        }
        return R.ok().put("result", result);
    }

    @GetMapping("searchCar")
    @ApiOperation("查询所有车辆信息")
    public R searchCar(){
        ArrayList<HashMap> result = carUseService.selectCar();
        return R.ok().put("result", result);
    }

    @PostMapping("/selectCarUseById")
    @ApiOperation("根据用车单ID查询用车单信息")
    public R selectLeaveById(@Valid @RequestBody DeleteReq req){
        HashMap result = carUseService.selectCarUseById(req.getId());
        return R.ok().put("result", result);
    }

    @DeleteMapping("/deleteCarUseById")
    @ApiOperation("根据用车单ID删除用车单")
    public R deleteCarUseById(@Valid @RequestBody DeleteReq req){
        int i = carUseService.deleteByid(req.getId());
        return R.ok().put("result","已删除"+i+"条记录");
    }

    @PostMapping("/carUseInfo")
    @ApiOperation("查询用车具体情况")
    public R carUseInfo(@Valid @RequestBody SearchMonthCarReq req, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        String start = req.getYear().toString() + "-" + req.getMonth().toString() + "-01 00:00:00";
        DateTime startTime = DateUtil.parseDate(start);
        DateTime endTime = DateUtil.endOfMonth(startTime);
        HashMap<String,Object> param=new HashMap<>();
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("carId", req.getCarId());
        ArrayList<HashMap> result= carUseService.searchCarUseInfo(param);
        Materials car = materialsMapper.selectCarByid(req.getCarId());
        int unused=0;
        int used=0;
        for (HashMap h:result
             ) {
            if (h.get("type").equals("已用")){
                used++;
            } else if(h.get("type").equals("未用")){
                unused++;
            }
        }
        HashMap info=new HashMap<>();
        info.put("used", used);
        info.put("unused",unused);
        return R.ok().put("result",result).put("car",car).put("info",info);
    }
}
